package com.lagou.ipersistence.builder;

import com.lagou.ipersistence.session.Configuration;
import com.lagou.ipersistence.type.TypeAliasRegistry;

/**
 * @author Yan Jiahong
 * Created on 2020/11/15
 */
public abstract class BaseBuilder {
    protected final Configuration configuration;
    protected final TypeAliasRegistry typeAliasRegistry;

    public BaseBuilder(Configuration configuration) {
        this.configuration = configuration;
        this.typeAliasRegistry = configuration.getTypeAliasRegistry();
    }

    public Configuration getConfiguration() {
        return configuration;
    }

    protected <T> Class<? extends T> resolveClass(String alias) {
        if (alias == null) {
            return null;
        }
        try {
            return resolveAlias(alias);
        } catch (Exception e) {
            throw new BuilderException("无法解析类, 原因: " + e, e);
        }
    }

    protected <T> Class<? extends  T> resolveAlias(String alias) {
        return typeAliasRegistry.resolveClass(alias);
    }
}
